/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_GB_MEDIA_END_USER_H
#define MK_GB_MEDIA_END_USER_H
#include "MkGb28181/MkGbEndUser.h"

typedef MkFunction<void(Uint32 ErrorCode, const MkString& SessionId)> MkMediaStreamResultCallback;
class CMkGbConsumer;
class CMkRtpUdpServer;
class MkApi CMkGbMediaEndUser : public CMkGbEndUser
{
public:
    CMkGbMediaEndUser(CMkEventPool* pEventPool, CMkTimer* pTimer);
    ~CMkGbMediaEndUser();

    Uint32 Start(const MkGbStartParam& Param, Uint16 RtpBeginPort);

    Uint32 Play(const MkString& SipUri, BOOL bTcpSip, BOOL bTcpRtp,Uint32 WndIndex, const MkMediaStreamResultCallback& ResultCb, const MkStreamStoppedCallback& StreamStoppedCb, const MkEsPacketCallback& EsPacketCb);

    Uint32 Play(const MkString& ChannelId, const MkString& PlayType, const CMkTime& BeginTime, const CMkTime& EndTime, Uint32 WndIndex, const MkMediaStreamResultCallback& ResultCb, const MkStreamStoppedCallback& StreamStoppedCb, const MkEsPacketCallback& EsPacketCb);

    Uint32 Publish(const MkString& SipUri, BOOL bTcpSip, BOOL bTcpRtp, CMkMediaSource* pMediaSource, const MkMediaStreamResultCallback& ResultCb, const MkStreamStoppedCallback& StreamStoppedCb);
    Uint32 Publish2(const MkString& SipUri, BOOL bTcpSip, BOOL bTcpRtp, CMkMediaSource* pMediaSource, const MkMediaStreamResultCallback& ResultCb, const MkStreamStoppedCallback& StreamStoppedCb);
    Uint32 Publish(const MkString& ChannelId, CMkMediaSource* pMediaSource, const MkMediaStreamResultCallback& ResultCb, const MkStreamStoppedCallback& StreamStoppedCb);
protected:
private:
    CMkRtpUdpServer* GetFreeRtpUdpServer();
    Uint32 OnPublisherResult(CMkMediaSource* pMediaSource, const MkMediaStreamResultCallback& ResultCb, CMkRtpUdpServer *pRtpUdpServer, Uint32 ResultErrorCode, const MkString& SessionId, const MkRtpParam& AnswerRtpParam, const EventResponseCallback& EventCb);
    Uint32 OnPlayResult(const MkMediaStreamResultCallback& ResultCb, CMkRtpUdpServer *pRtpUdpServer, Uint32 ResultErrorCode, const MkString& SessionId, const MkRtpParam& AnswerRtpParam,const MkEsPacketCallback& EsPacketCb, const EventResponseCallback& EventCb);
    Uint32 RemoveConsumer(const MkString& SessionId);
    Uint32 RemovePlayer(const MkString& SessionId);
private:
    Uint16                          m_RtpBeginPort;
    CMkRwLock                       m_GbConsumerLock;
    MkMap<MkString, CMkGbConsumer*> m_MapConsumer;
    CMkRwLock                       m_GbPlayLock;
    MkMap<MkString, CMkPsStreamSource*> m_MapGbPlayer;
    MkVector<CMkRtpUdpServer*>      m_VecRtpUdpServer;
};

#endif